package com.tping.leetcode.array;

/**
 * 统计一个数字在排序数组中出现的次数。
 * 
 * 输入: nums = [5,7,7,8,8,10], target = 8
 * 输出: 2
 * 
 */
public class Solution_53 {

    public static void main(String[] args){

        int[] nums = new int[]{5,7,7,8,8,10};
        int target = 8;
        int count = helper(nums, target) - helper(nums, target - 1);
        System.out.println("在排序数组中出现的次数: " + count);
    }

    private static int helper(int[] nums, int target) {
        int i = 0;
        int j = nums.length -1;
        while (i <= j){
            int m = (i + j) / 2;
            if( nums[m] <= target ){
                i = m + 1;
            }else{
                j = m -1;
            }
        }
        return i;
    }

}
